# Maintainer: Saul Ibarra Corretge <saghul@gmail.com>
# Contributor: Alexey Pavlov <alexpux@gmail.com>

_realname=sip
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}" "${MINGW_PACKAGE_PREFIX}-python2-${_realname}" "${MINGW_PACKAGE_PREFIX}-python3-${_realname}")
pkgver=4.19.9
#_pre_ver=4.19.1.dev1702081834
pkgrel=1
pkgdesc="Tool to create Python bindings for C and C++ libraries (mingw-w64)"
arch=('any')
license=('GPL')
url="http://www.riverbankcomputing.co.uk/software/sip"
makedepends=("${MINGW_PACKAGE_PREFIX}-gcc"
             "${MINGW_PACKAGE_PREFIX}-python2"
             "${MINGW_PACKAGE_PREFIX}-python3")
source=("https://downloads.sourceforge.net/project/pyqt/${_realname}/${_realname}-${pkgver}/${_realname}-${pkgver}.tar.gz"
        #https://www.riverbankcomputing.com/static/Downloads/sip/sip-${_pre_ver}.tar.gz
        "0001-mingw-python.patch"
        "0002-sip-fix-build.patch")
sha256sums=('38a646ba483821ca4a6be7fa33e8634eb74812114e7081eccc4c3a8a6c92e438'
            '2e3400c7467faca17b6efaae2e2420b0a253df9070b1ed0adcafcc10f9499a8d'
            'b76b3eb3615ffa8dfd017f533214718812de22067802a749b32d9e2e84abf233')

prepare() {
  cd "${srcdir}"/${_realname}-${pkgver}

  # Apply patches
  patch -p1 -i "${srcdir}"/0001-mingw-python.patch
  patch -p1 -i "${srcdir}"/0002-sip-fix-build.patch
}

build() {
  for builddir in python{2,3}-${MINGW_CHOST}; do
    [[ -d $builddir ]] && rm -rf ${builddir}
    cp -r ${_realname}-${pkgver} ${builddir}
    pushd ${builddir}
    ${MINGW_PREFIX}/bin/${builddir%-${MINGW_CHOST}} configure.py \
      CFLAGS="${CFLAGS}" LFLAGS="${LDFLAGS}"
    make
    popd
  done
}

package_sip() {
  pkgdesc="A tool that makes it easy to create Python bindings for C and C++ libraries (mingw-w64)"
  depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs")

  cd python2-${MINGW_CHOST}
  make DESTDIR="${pkgdir}" install -C sipgen

  install -Dm644 LICENSE "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE"
}

package_python3-sip() {
  pkgdesc="Python 3.x SIP bindings for C and C++ libraries (mingw-w64)"
  depends=("${MINGW_PACKAGE_PREFIX}-sip" "${MINGW_PACKAGE_PREFIX}-python3")

  cd python3-${MINGW_CHOST}
  make DESTDIR="${pkgdir}" install -C siplib

  local _py3basever=$(${MINGW_PREFIX}/bin/python3 -c "import sys;sys.stdout.write('.'.join(map(str, sys.version_info[:2])))")

  install -Dm644 sipconfig.py "${pkgdir}${MINGW_PREFIX}"/lib/python${_py3basever}/site-packages/sipconfig.py
  install -Dm644 sipdistutils.py "${pkgdir}${MINGW_PREFIX}"/lib/python${_py3basever}/site-packages/sipdistutils.py

  install -Dm644 LICENSE "${pkgdir}${MINGW_PREFIX}/share/licenses/python3-sip/LICENSE"
}

package_python2-sip() {
  pkgdesc="Python 2.x SIP bindings for C and C++ libraries (mingw-w64)"
  depends=("${MINGW_PACKAGE_PREFIX}-sip" "${MINGW_PACKAGE_PREFIX}-python2")

  cd python2-${MINGW_CHOST}
  make DESTDIR="${pkgdir}" install -C siplib

  local _py2basever=$(${MINGW_PREFIX}/bin/python2 -c "import sys;sys.stdout.write('.'.join(map(str, sys.version_info[:2])))")

  install -Dm644 sipconfig.py "${pkgdir}${MINGW_PREFIX}"/lib/python${_py2basever}/site-packages/sipconfig.py
  install -Dm644 sipdistutils.py "${pkgdir}${MINGW_PREFIX}"/lib/python${_py2basever}/site-packages/sipdistutils.py

  install -Dm644 LICENSE "${pkgdir}${MINGW_PREFIX}/share/licenses/python2-sip/LICENSE"
}

package_mingw-w64-i686-sip() {
  package_sip
}

package_mingw-w64-i686-python2-sip() {
  package_python2-sip
}

package_mingw-w64-i686-python3-sip() {
  package_python3-sip
}

package_mingw-w64-x86_64-sip() {
  package_sip
}

package_mingw-w64-x86_64-python2-sip() {
  package_python2-sip
}

package_mingw-w64-x86_64-python3-sip() {
  package_python3-sip
}
